package com.studyroom.utils;

import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

public class FileUploadUtil {
    
    private static final String UPLOAD_PATH = "uploads/s/"; // 相对于项目根目录
    
    public static String uploadImage(MultipartFile file) throws IOException {
        // 确保目录存在
        File uploadDir = new File(UPLOAD_PATH);
        if (!uploadDir.exists()) {
            uploadDir.mkdirs();
        }
        
        // 生成唯一文件名
        String originalFilename = file.getOriginalFilename();
        String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
        String filename = UUID.randomUUID().toString() + extension;
        
        // 保存文件
        File dest = new File(uploadDir.getAbsolutePath() + File.separator + filename);
        file.transferTo(dest);
        
        // 返回可访问的URL路径
        return "/uploads/s/" + filename;
    }
} 